package com.wowangz.cms.common.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.wowangz.cms.common.pojo.DataDictionaryInfo;
import com.wowangz.cms.common.pojo.SysDataDictionary;
import com.wowangz.cms.common.pojo.SysDataDictionaryValue;
import com.wowangz.cms.common.select.BaseSelect;
import com.wowangz.cms.common.select.SelectFactory;
import com.wowangz.cms.common.service.SysDataDictionaryService;
import com.wowangz.cms.syscore.IssTransactionContainer;
import com.wowangz.cms.syscore.exception.ServiceException;

/**
 * 
 * 管理和获取数据字典
 * 
 * @author leiyang3
 *
 */
public class DataDictionaryManage {

	private static final Logger logger = LoggerFactory.getLogger(DataDictionaryManage.class);

	private static Map<String, List<DataDictionaryInfo>> dataDictionaryMap = null;

	// 获取整个数据字典
	public final static Map<String, List<DataDictionaryInfo>> getDataDictionaryMap() {

		if (dataDictionaryMap == null) {
			DataDictionaryManage.initDataDictionary();
		}

		return dataDictionaryMap;
	}

	// 获取单个数据字典
	public final static List<DataDictionaryInfo> getDataDictionaryList(String dictTypeCode) {

		if (dataDictionaryMap == null) {
			DataDictionaryManage.initDataDictionary();
		}

		return dataDictionaryMap.get(dictTypeCode);
	}

	/**
	 * 
	 * 根据字段属性编号值获取单个数据字典
	 * 
	 * @param dictTypeCode
	 * @param dictCode
	 * @return
	 */
	public final static DataDictionaryInfo getDataDictionaryInfo(String dictTypeCode, String dictCode) {

		DataDictionaryInfo info = null;

		if (dataDictionaryMap == null) {
			DataDictionaryManage.initDataDictionary();
		}

		List<DataDictionaryInfo> list = dataDictionaryMap.get(dictTypeCode);
		if (list != null) {
			for (int i = 0; i < list.size(); i++) {
				info = list.get(i);

				if (info.getDictCode().equals(dictCode)) {
					return info;
				}
			}
		}

		return null;
	}

	/**
	 * 
	 * 根据字段属性名称值获取单个数据字典
	 * 
	 * @param dictTypeCode
	 * @param dictName
	 * @return
	 */
	public final static DataDictionaryInfo getDataDictionaryInfoByName(String dictTypeCode, String dictName) {

		DataDictionaryInfo info = null;

		if (dataDictionaryMap == null) {
			DataDictionaryManage.initDataDictionary();
		}

		List<DataDictionaryInfo> list = dataDictionaryMap.get(dictTypeCode);
		if (list != null) {
			for (int i = 0; i < list.size(); i++) {
				info = list.get(i);

				if (info.getDictName().equals(dictName)) {
					return info;
				}
			}
		}

		return null;
	}

	/**
	 * 
	 * 获取单个数据字典的名称
	 * 
	 * @param dictTypeCode
	 * @param dictCode
	 * @return
	 */
	public final static String getDataDictionaryName(String dictType, String dictCode) {

		String dictName = "";
		DataDictionaryInfo info = null;

		if (dataDictionaryMap == null) {
			DataDictionaryManage.initDataDictionary();
		}

		List<DataDictionaryInfo> list = dataDictionaryMap.get(dictType);
		if (list != null) {
			for (int i = 0; i < list.size(); i++) {
				info = list.get(i);

				if (info.getDictCode().equals(dictCode)) {
					dictName = info.getDictName();
				}
			}
		}

		return dictName;
	}

	/**
	 * 
	 * 获取单个数据字典的名称
	 * 
	 * @param dictTypeCode
	 * @param dictCode
	 * @return
	 */
	public final static String getDataDictionaryName(String dictTypeCode, Long dictCode) {
		String dictName = "";
		DataDictionaryInfo info = null;

		if (dataDictionaryMap == null) {
			DataDictionaryManage.initDataDictionary();
		}

		List<DataDictionaryInfo> list = dataDictionaryMap.get(dictTypeCode);
		if (list != null) {
			for (int i = 0; i < list.size(); i++) {
				info = list.get(i);

				if (info.getDictCode().equals(String.valueOf(dictCode))) {
					dictName = info.getDictName();
				}
			}
		}

		return dictName;
	}

	public final static String getDataDictionaryName(String dictTypeCode, long dictCode) {
		String dictName = "";
		DataDictionaryInfo info = null;

		if (dataDictionaryMap == null) {
			DataDictionaryManage.initDataDictionary();
		}

		List<DataDictionaryInfo> list = dataDictionaryMap.get(dictTypeCode);
		if (list != null) {
			for (int i = 0; i < list.size(); i++) {
				info = list.get(i);

				if (info.getDictCode().equals(String.valueOf(dictCode))) {
					dictName = info.getDictName();
				}
			}
		}

		return dictName;
	}

	/**
	 * 初始化方法 区分数据源
	 * 
	 * @throws ServiceException
	 */
	public final static void initDataDictionary() throws ServiceException {
		DataDictionaryManage.initDBDataDictionary();
	}

	/**
	 * 
	 * 初始化DataDictionary
	 * 
	 */
	private static void initDBDataDictionary() throws ServiceException {

		logger.debug("Loading DataDictionary from database");

		List<DataDictionaryInfo> list = null;
		DataDictionaryInfo dataDictionaryInfo = null;

		List<SysDataDictionary> sysDataDictionaryList = null;
		List<SysDataDictionaryValue> sysDataDictionaryValueList = null;

		try {
			// 数据字典从公共的数据库取得数据
			SysDataDictionaryService sysDataDictionaryService = (SysDataDictionaryService) IssTransactionContainer.getInstance(SysDataDictionaryService.class, false);
			sysDataDictionaryList = sysDataDictionaryService.queryDataDictionaryList();
			sysDataDictionaryValueList = sysDataDictionaryService.queryDataDictionaryValueList();
		} catch (Exception e) {
			throw new ServiceException("数据库初始化数据字典异常", e);
		}
		if (sysDataDictionaryList == null || sysDataDictionaryList.isEmpty()) {
			throw new ServiceException("查询数据字典类型信息失败，数据库初始化数据字典失败！");
		}
		if (sysDataDictionaryValueList == null || sysDataDictionaryValueList.isEmpty()) {
			throw new ServiceException("查询数据字典明细信息失败，数据库初始化数据字典失败！");
		}

		try {
			dataDictionaryMap = new HashMap<String, List<DataDictionaryInfo>>();

			for (SysDataDictionary sysDataDictionary : sysDataDictionaryList) {
				String dictType = sysDataDictionary.getDictType();
				list = new ArrayList<DataDictionaryInfo>();
				for (SysDataDictionaryValue sysDataDictionaryValue : sysDataDictionaryValueList) {
					String dictTypeOfValue = sysDataDictionaryValue.getDictType();
					if (dictTypeOfValue.equals(dictType)) {

						dataDictionaryInfo = new DataDictionaryInfo();
						dataDictionaryInfo.setDictType(dictType);
						dataDictionaryInfo.setDictCode(sysDataDictionaryValue.getDictCode());
						dataDictionaryInfo.setDictName(sysDataDictionaryValue.getDictName());
						dataDictionaryInfo.setDictAttr(sysDataDictionaryValue.getDictAttr());
						dataDictionaryInfo.setDictDesc(sysDataDictionaryValue.getDictDesc());
						list.add(dataDictionaryInfo);
					}
				}
				dataDictionaryMap.put(dictType, list);
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException("数据库初始化数据字典异常", e);
		}
	}

	public static void main(String[] args) {

		Map<String, List<DataDictionaryInfo>> map = DataDictionaryManage.getDataDictionaryMap();
		List<DataDictionaryInfo> list = null;
		DataDictionaryInfo dataDictionaryInfo = null;

		Iterator keyIterator = map.keySet().iterator();
		while (keyIterator.hasNext()) {
			list = map.get(keyIterator.next());

			for (int i = 0; i < list.size(); i++) {
				dataDictionaryInfo = list.get(i);
				System.out.println(dataDictionaryInfo.toString());
			}
		}

	}
}
